<?php
namespace App\Model;

use lib\Model;

class User extends Model
{
    private $id;
    private $email;
    private $userName;
    private $password;

    // private static $foo;

    protected $dataDirectory = 'user';

    /**
     * 根据数组中的数据，先验证数据，验证通过则返回User对象实例
     */
    public static function newUser(Array $data)
    {
        $password_confirm = isset($data['password_confirm']) ? $data['password_confirm'] : '';

        $result = self::validate($data['id'], $data['email'], $data['userName'], $data['password'], $password_confirm);
        if ($result === true) {
            return self::fromArray($data);
        }

        return null;
    }

    public function gallaries()
    {
        return Gallary::get(['user.id' => $this->id]);
    }

    public static function fromArray(Array $array)
    {
        if (empty($array) || count($array) == 0) {
            return null;
        }

        $user = new User();
        $user->id = $array['id'];
        $user->email = $array['email'];
        $user->userName = $array['userName'];
        $user->password = $array['password'];
        return $user;
    }

    public function toArray()
    {
        return [
            'id' => $this->id,
            'email' => $this->email,
            'userName' => $this->userName,
            'password' => $this->password
        ];
    }

    /**
     * 验证用户信息的数据有效性，使用静态方法
     * 静态方法: 不能通过对象实例调用，只能通过类名::调用
     * 例如 self::validate(), User::validate()
     * 静态方法，不能调用实例特征，只能访问静态特征
     *
     * @return bool 验证通过，返回true，否则返回false
     */
    private static function validate($id, $email, $userName, $password, $password_confirm = '')
    {
        $isValid = true;
        $errorBag = ['id' => '', 'email' => '', 'userName' => '', 'password' => '', 'password_confirm' => ''];
        $inputBag = ['id' => $id, 'email' => $email, 'userName' => $userName,];

        if (!preg_match('/^[1-9]+\d*$/', $id)) {
            $isValid = false;
            $errorBag['id'] = 'id只能是整数';
        }
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $isValid = false;
            $errorBag['email'] = '邮箱不合法';
        }
        if (!preg_match('/^[\w\x{4e00}-\x{9fa5}]+$/u', $userName)) {
            $isValid = false;
            $errorBag['userName'] = '请输入合法的用户名';
        }
        if (strlen($password) < 6) {
            $isValid = false;
            $errorBag['password'] = '密码长度必须大于等于6位';
        }
        if ($password !== $password_confirm) {
            $isValid = false;
            $errorBag['password_confirm'] = '两次输入的密码必须一致';
        }

        if (session_status() != PHP_SESSION_ACTIVE) {
            session_start();
        }
        $_SESSION['error_bag'] = $errorBag;
        $_SESSION['input_bag'] = $inputBag;

        return $isValid;
    }

    public function __get($name)
    {
        if (property_exists(self::class, $name)) {
            return $this->$name;
        }
        return null;
    }

    public function __set($name, $value)
    {
        if (property_exists(self::class, $name)) {
            $this->$name = $value;
        }
    }

    public function __tostring()
    {
        return 'id: ' . $this->id . ' 邮箱: ' . $this->email . ' 用户名: ' . $this->userName . '<br/>';
    }
}
